//
//  YARefreshControl.swift
//  KeepCliming
//
//  Created by 鱼龙之梦 on 15/9/8.
//  Copyright © 2015年 panyanzhi. All rights reserved.
//

import UIKit
private let YARefreshControlMaxOffSet:CGFloat = -60
class YARefreshControl: UIRefreshControl {
    private lazy var refreshView = YARefreshControlView.refreshView()
    override func endRefreshing() {
        super.endRefreshing()
        refreshView.stopAnmation()
    }
    //添加子控件 布局元素
    private func setupUI(){
       self.addObserver(self, forKeyPath: "frame", options: [], context: nil)
       tintColor = UIColor.clearColor()
       addSubview(refreshView)
       refreshView.ff_AlignInner(type: ff_AlignType.CenterCenter, referView: self, size: refreshView.bounds.size)
    }
    deinit {
        self.removeObserver(self, forKeyPath: "frame")
    }
     //MARK:--- 构造函数
    override init()
    {
        super.init()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init()
        setupUI()
    }
    //MARK:--- KVO监听方法
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
      //  printLog(frame.origin.y)
        if  frame.origin.y > 0 {
            return
        }
       //  printLog("中\(refreshing)")
        if refreshing{
          //  printLog("刷新中\(frame.origin.y)")
            refreshView.loadingAnimation()
            return
        }
        //临界点是-60，如果在临界点下面 同时 箭头朝下
        if frame.origin.y < YARefreshControlMaxOffSet && !refreshView.rotateFlag {
            refreshView.rotateFlag = true //下次箭头朝上
        }else if frame.origin.y >= YARefreshControlMaxOffSet && refreshView.rotateFlag {
            refreshView.rotateFlag = false //下次箭头朝下
        }
    }
}

//刷新视图，单独负责显示内容&动画

class YARefreshControlView: UIView {

    @IBOutlet weak var tipView: UIView!
    
    @IBOutlet weak var pullView: UIImageView!
    
    @IBOutlet weak var loadingView: UIImageView!
    
///  负责从xib 加载视图
    class func refreshView()->YARefreshControlView {
        return NSBundle.mainBundle().loadNibNamed("YARefreshControl", owner: nil, options: nil).last! as! YARefreshControlView
    }
    private var rotateFlag = false {
        didSet{
            rotateTipAnimation()
        }
    }
///  旋转提示图标动画
    private func rotateTipAnimation(){
        var angle = CGFloat(M_PI)
        angle += rotateFlag ? -0.01 : 0.01
        UIView.animateWithDuration(0.5){
            self.pullView.transform = CGAffineTransformRotate(self.pullView.transform, angle)
        }
    }
///  加载动画
    private func loadingAnimation(){
        let loadingKey = "loadingKey"
        if loadingView.layer.animationForKey(loadingKey) != nil {
            return
        }
        tipView.hidden = true
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 1
        loadingView.layer.addAnimation(anim, forKey: loadingKey)
    }
///  停止动画
    private func stopAnmation(){
        tipView.hidden = false
        loadingView.layer.removeAllAnimations()
    }
}
